package com.mico.echart.monitor.system;

import java.text.DecimalFormat;

/**
 * @author micocube
 * projectName: utils4j
 * packageName: com.coding.system
 * email: ldscube@gmail.com
 * createTime: 2019-12-30 09:09
 * version: 0.1
 * description:文件大小，变为易读的文件大小
 */
public class FileSize {
    /**
     * 1024bit
     *
     */
    private static long B_SIZE = 2 << 9;
    /**
     * 1Kb
     */
    private static long KB_SIZE = 1 * 1024 * B_SIZE;
    /**
     * 1Mb
     */
    private static long MB_SIZE = 1 *  1024 * KB_SIZE;
    /**
     * 1Gb
     */
    private static long GB_SIZE = 1 *  1024 * MB_SIZE;




    public static String formatFileSize(long fileBytes) {
        DecimalFormat df = new DecimalFormat("#0.00");
        String wrongSize = "0B";
        if (fileBytes == 0) {
            return wrongSize;
        }
        if(fileBytes<0){
            return fileBytes+"";
        }
        String fileSize;
        if (fileBytes < B_SIZE) {
            fileSize = df.format((double) fileBytes) + "B";
        } else if (fileBytes < KB_SIZE) {
            fileSize = df.format((double) fileBytes / B_SIZE) + "KB";
        } else if (fileBytes < MB_SIZE) {
            fileSize = df.format((double) fileBytes / KB_SIZE) + "MB";
        } else if(fileBytes<GB_SIZE){
            fileSize = df.format((double) fileBytes / MB_SIZE) + "GB";
        }else {
            fileSize = df.format((double) fileBytes / GB_SIZE) + "TB";
        }
        return fileSize;
    }

    public static void main(String[] args) {
        String s = formatFileSize(2147483648L );
        System.out.println(s);
    }

}
